package com.example.tms.repository.neo4j;

import com.example.tms.dto.CollaboratorDTO;
import com.example.tms.dto.ColleagueDTO;
import com.example.tms.entity.EmployeeNode;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmployeeGraphRepository extends Neo4jRepository<EmployeeNode, String> {

    // 查询管理链条
    @Query("MATCH path = (e:Employee {id: $id})<-[:MANAGES*]-(sub) RETURN nodes(path)")
    List<List<EmployeeNode>> findManagementChain(@Param("id") String employeeId);

    // 查询项目协作网络
    @Query("""
        MATCH (e:Employee {id: $id})-[:PARTICIPATED_IN]->(p)<-[:PARTICIPATED_IN]-(colleague)
        RETURN colleague, COLLECT(p.name) as commonProjects""")
    List<CollaboratorDTO> findCollaborators(@Param("id") String employeeId);

    // 查询同事关系网络
    @Query("""
    MATCH (e:Employee {id: $id})-[:COLLEAGUE]-(colleague)
    RETURN colleague, 
           COLLECT([(colleague)-[r:COLLEAGUE]-(e) | r.department][0]) as commonDepartments""")
    List<ColleagueDTO> findColleagues(@Param("id") String employeeId);

}


